let index = 1;
import { createMonthArray, createWeekArray } from '@/utils/time';
import moment from 'moment';

export function createMonthObj(timeRange = []) {
  const ranges = createMonthArray(timeRange);
  return ranges.reduce((pre, cur) => {
    pre[cur] = '';
    return pre;
  }, {});
}

export function createWeekObj(timeRange = []) {
  const ranges = createWeekArray(timeRange);
  const month = ranges.reduce((pre, cur) => {
    if (pre[cur.month]) {
      pre[cur.month].push(cur);
    } else {
      pre[cur.month] = [cur];
    }
    return pre;
  }, {});
  return Object.keys(month).reduce((pre, cur) => {
    pre[cur] = month[cur].reduce((p, c) => {
      p[c.week] = '';
      return p;
    }, {});
    return pre;
  }, {});
}

export function transferDataDetail(list, timeRange = []) {
  const reg = /^[+]{0,1}(\d+)/;

  return list.map(item => {
    const price = JSON.parse(item.price || item.executeForecast || '{}');

    const fcst = JSON.parse(
      item.fcst ||
        item.preAllocated ||
        item.supplyPlan ||
        item.salesForecast ||
        '{}'
    );
    const lastFcst = JSON.parse(item.lastFcst || '{}');

    const defaultObj = createMonthObj(timeRange);
    const defaultWeekObj = createWeekObj(timeRange);
    if (!reg.test(item.modelCutRate)) {
      item.modelCutRate = '';
    }
    return {
      ...item,
      id: item.id ? item.id : `random_${++index}`,
      customerCode: {
        key: item.customerCode,
        label: item.customerName,
      },
      modelName: {
        key: item.modelCode,
        label: item.modelName,
      },
      modelNumber: {
        key: item.modelNumber,
        label: item.modelInfoName,
      },
      price: { ...defaultObj, ...price },
      fcst: { ...defaultWeekObj, ...fcst },
      lastFcst: { ...lastFcst },
      // modelCutRate: Number(item.modelCutRate) > 0 ? item.modelCutRate : '',
      // ...defaultObj,
      // ...priceFormat,
      // ...fcstFormat,
      // ...temp,
    };
  });
}

const createDayObj = ([startTime, endTime], value = {}) => {
  const obj = {};
  Object.values(value).map(item => {
    Object.entries(item).forEach(([key, v]) => {
      obj[key] = v;
    });
  });
  console.log('obj: ', obj);

  const monthStart = moment(startTime).startOf('month');
  const monthEnd = moment(endTime).endOf('month');

  const diff = monthEnd.diff(monthStart, 'day') + 1;
  const list = new Array(diff).fill(1).map((item, index) => {
    const date = monthStart.clone().add(index, 'days');
    return {
      weekYear: date.weekYear(),
      week: date.week(),
      month: date.format('YYYY-MM'),
      date: date.format('YYYY-MM-DD'),
      value: obj[date.format('YYYY-MM-DD')],
    };
  });
  console.log('list: ', list);
  const month = list.reduce((pre, cur) => {
    if (pre[cur.month]) {
      pre[cur.month].push(cur);
    } else {
      pre[cur.month] = [cur];
    }
    return pre;
  }, {});
  Object.keys(month).map(item => {
    month[item] = month[item].reduce((pre, cur) => {
      if (pre[cur.week]) {
        pre[cur.week].push(cur);
      } else {
        pre[cur.week] = [cur];
      }
      return pre;
    }, {});
  });
  Object.keys(month).map(week => {
    Object.keys(month[week]).forEach(item => {
      month[week][item] = month[week][item].reduce((pre, cur) => {
        pre[cur.date] = cur.value;
        return pre;
      }, {});
    });
  });
  console.log('month: ', month);
  return month;
};

export function transferSupplyList(list, timeRange = []) {
  const reg = /^[+]{0,1}(\d+)/;

  return list.map(item => {
    const price = JSON.parse(item.price || item.executeForecast || '{}');

    const fcst = JSON.parse(
      item.fcst ||
        item.preAllocated ||
        item.supplyPlan ||
        item.salesForecast ||
        '{}'
    );

    const defaultObj = createMonthObj(timeRange);
    const defaultWeekObj = createWeekObj(timeRange);
    // const dayFcst = createDayObj(timeRange, fcst);
    if (!reg.test(item.modelCutRate)) {
      item.modelCutRate = '';
    }
    return {
      ...item,
      id: item.id ? item.id : `random_${++index}`,
      customerCode: {
        key: item.customerCode,
        label: item.customerName,
      },
      modelName: {
        key: item.modelCode,
        label: item.modelName,
      },
      modelNumber: {
        key: item.modelNumber,
        label: item.modelInfoName,
      },
      price: { ...defaultObj, ...price },
      fcst: { ...defaultWeekObj, ...fcst },
    };
  });
}
